<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class OfflinePay
{
    protected $id;

    public function __construct($oid, $title, $product_type)
    {
        $this->oid = $oid;
        $this->title = $title;
        $this->product_type = $product_type;
    }

    protected function script()
    {
        return <<<SCRIPT

layui.use('layer', function(){
        var layer = layui.layer;
        $('.offlinepay').click(function () {
           var that = this;
           var oid = $(that).attr('data-oid');
           var title = $(that).attr('data-title');
           var product_type = $(that).attr('data-type');
           if(product_type == 2){
                var index = layer.open({
                    title: '<\span style="color:red;">'+title+'<\/span>',
                    type: 2,
                    content: '/admin/common/order/divide/payall?oid='+oid,
                    area: ['40%','70%'],
                    btn: ['修改为已还款','取消'],
                    yes: function(index, layero){
                        var layer_obj = $(layero).find("iframe").contents();
                        var divide = layer_obj.find("input[name=divide]");
                        var account = {};
                        divide.each(function(index, element){
                            var val = $(this).val();
                            var id = $(this).attr('data-id');
                            account[id] = val;
                        });
                        var paymethod = layer_obj.find("select[name=paymethod]").val();
                       
                        for(var prop in account){
                             var patrn = /^(-)?\d+(\.\d+)?$/;
                             if(account[prop] && !patrn.test(account[prop])){
                                layer.msg('销账内容必须为数字');
                                return;
                             }
                        }

                        var divide_json =  JSON.stringify(account)
                       
                        $.ajax({
                            url: '/admin/common/order/divide/payall',
                            type: 'post',
                            data: {
                                divide_json: divide_json,
                                method: paymethod,
                                oid: oid
                            },
                            success:function(res){
                                if(res.code == -1){
                                    layer.msg(res.text);
                                    return;
                                }
                                layer.msg(res.text);
                                layer.close(index);
                                $(".container-refresh").click();
                            }
                        })
                    },
                    btn2: function(index, layero){
                        layer.close(index);
                    }
                });
           
                return;
           };
           
           var html = '';
           html += '<\div style="padding:0 30px;">';
           html += '<\div style="margin-top:16px;"><\span style="text-align:left;margin-right:10px;display:inline-block;width: 24%;">还款金额<\/span><\input name="money" style="width:71%;"><\/div>';
           html += '<\div style="margin-top:10px;"><\span style="text-align:left;margin-right:10px;display:inline-block;width: 24%;">还款方式<\/span>';
           html += '<\select name="ordermethod" style="width:71%;display:inline-block;">';
           html += '<\option value="支付宝">支付宝<\/option>';
           html += '<\option value="微信">微信<\/option>';
           html += '<\/select>';
           html += '<\/div>';
           html += '<\/div>';
           var index = layer.open({
                title: '<\span style="color:red;">'+title+'<\/span>',
                type: 1,
                content: html,
                area: ['350px','170px'],
                btn: ['修改为已还款','取消'],
                yes: function(){
                    var money = $('input[name=money]').val();
                    var method = $('select[name=ordermethod]').val();
                    var state = 3;
                    var reg=/^[0-9]+.?[0-9]*$/;
                    if(!reg.test(money)){
                        layer.msg('付款方式格式不正确！');
                        return;
                    }
                    $.ajax({
                        url: '/admin/offline/pay/state',
                        type: 'post',
                        data: {
                            oid: oid,
                            money: money,
                            method: method,
                            state: state
                        },
                        success:function(res){
                            layer.msg(res.info);
                            layer.close(index);
                            location.href = location.href;
                        }
                    })
                },
                btn2: function(index, layero){
                    layer.close(index);
                }
            });
       });
       
        $('.offlineextend').click(function () {
           var that = this;
           var oid = $(that).attr('data-oid');
           var title = $(that).attr('data-title');
           var html = '';
           html += '<\div style="padding:0 30px;">';
           html += '<\div style="margin-top:10px;"><\span style="text-align:left;margin-right:10px;display:inline-block;width: 24%;">付款金额<\/span><\input name="money" style="width:71%;"><\/div>';
           html += '<\div style="margin-top:10px;"><\span style="text-align:left;margin-right:10px;display:inline-block;width: 24%;">付款方式<\/span>';
           html += '<\select name="extendmethod" style="width:71%;display:inline-block;">';
           html += '<\option value="支付宝">支付宝<\/option>';
           html += '<\option value="微信">微信<\/option>';
           html += '<\/select>';
           html += '<\/div>';
           html += '<\div style="margin-top:10px;"><\span style="text-align:left;margin-right:10px;display:inline-block;width: 24%;">展期天数<\/span><\input name="extime" style="width:71%;padding-left:5px;" placeholder="请输入展期天数"><\/div>';
           html += '<\/div>';
           var index = layer.open({
                title: '<\span style="color:red;">'+title+'<\/span>',
                type: 1,
                content: html,
                area: ['300px','200px'],
                btn: ['确定','取消'],
                yes: function(){
                    var money = $('input[name=money]').val();
                    var method = $('select[name=extendmethod]').val();
                    var extime = $('input[name=extime]').val();
                    var reg=/^[0-9]+.?[0-9]*$/;
                    if(!reg.test(money)){
                        layer.msg('付款金额格式不正确！');
                        return;
                    }
                    var ext=/^[0-9]+$/;
                    if(!ext.test(extime)){
                        layer.msg('展期天数格式不正确！');
                        return;
                    }
                    $.ajax({
                        url: '/admin/offline/pay/extend',
                        type: 'post',
                        data: {
                            oid: oid,
                            money: money,
                            method: method,
                            extime: extime
                        },
                        success:function(res){
                            layer.msg(res.info);
                            layer.close(index);
                            if(res.code == 0){
                                location.href = location.href;
                            }
                        }
                    })
                },
                btn2: function(index, layero){
                    layer.close(index);
                }
            });
       });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        $xiao = "&nbsp;<a href='javascript:void(0);' ><button  data-oid='{$this->oid}' data-title='{$this->title}' data-type='{$this->product_type}' class='offlinepay layui-btn  layui-btn-normal layui-btn-xs'>销</button></a>";

        $zhan = "&nbsp;<a href='javascript:void(0);' ><button  data-oid='{$this->oid}' data-title='{$this->title}'  class='offlineextend layui-btn  btn-info layui-btn-xs'>展</button></a>";
        
        $btn = $xiao.$zhan;

        return $btn;
    }

    public function __toString()
    {
        return $this->render();
    }
}